_realname=rtl-sdr
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.8.0
pkgrel=1
pkgdesc="Driver for Realtek RTL2832U, allowing general purpose software defined radio (SDR) (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://sdr.osmocom.org/trac/wiki/rtl-sdr"
license=('GPL')
depends=("${MINGW_PACKAGE_PREFIX}-libusb>=1.0"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=("rtl-sdr.$pkgver.tgz::https://github.com/librtlsdr/librtlsdr/archive/v$pkgver.tar.gz"
        "001-sys-time.patch")
sha512sums=('afef8508ccc1313470e8f5440fd7104298be23737eeae03954be912f93b6000317ec354412939c95956d3958ec4bf6e437d5ddb8817479b210dd5da544fc0a47'
            '8cb9621edb363b6db5cfa022d2cc923c3cf5d662a19d47778a7968f5e6f2eb54d87f680ffc80c22bd9b42cc417e3974ae7391284a5d19fb4c4936111e69a8a99')

prepare() {
  cd "$srcdir/librtlsdr-$pkgver"

  patch -Np0 -i "${srcdir}/001-sys-time.patch"
}

build() {
  cd "$srcdir/librtlsdr-$pkgver"

  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DDETACH_KERNEL_DRIVER=ON -Wno-dev \
      "${extra_config[@]}" \
      -GNinja \
      ../librtlsdr-$pkgver

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
}
